package com.woniuxy.oldmanapi.controller;

import com.woniuxy.oldmanapi.entity.Storey;
import com.woniuxy.oldmanapi.service.StoreyService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class StoreyController {

    @Autowired
    StoreyService storeyService;


    @GetMapping("/storey/list/{id}")
    public ResponseResult<List<Storey>> list(@PathVariable("id") int id) {
        ResponseResult<List<Storey>> result = new ResponseResult<>();
        try {
            List<Storey> lists = storeyService.findStoreys(id);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(lists);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }
    @GetMapping("/storey/num/{id}")
    public ResponseResult<Integer> num(@PathVariable("id") int id) {
        ResponseResult<Integer> result = new ResponseResult<>();
        try {
            int a = storeyService.findStoreyNum(id);
            result.setCode(200);
            result.setMsg("查数成功!");
            result.setData(a);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查数失败!");
        }
        return  result;
    }

    @GetMapping("/storey/selectByBuildIdYSW/{buildingId}")
    public ResponseResult<List<Storey>> selectByBuildIdYSW(@PathVariable("buildingId") Integer buildingId) {
        ResponseResult<List<Storey>> result = new ResponseResult<>();
        try {
            List<Storey> list = storeyService.selectByBuildIdYSW(buildingId);
            if (list != null) {
                result.setCode(200);
                result.setData(list);
                result.setMsg("查询成功");
            } else {
                result.setCode(204);
                result.setMsg("该楼栋未录入楼层");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("系统繁忙,请稍后再试");
            result.setCode(205);

        }
        return result;
    }

    @PostMapping("/storey/add")
    public ResponseResult<Void> add(@RequestBody Storey storey) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            storeyService.add(storey);
            result.setCode(200);
            result.setMsg("添加成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败!");
        }
        return result;
    }

    @PutMapping("/storey/modify")
    public ResponseResult<Void> modify(@RequestBody Storey storey) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            storeyService.modify(storey);
            result.setCode(200);
            result.setMsg("修改成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("修改失败!");
        }
        return result;
    }

    @DeleteMapping("/storey/delete/{id}")
    public ResponseResult<Void> delete(@PathVariable("id") int id) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            storeyService.delete(id);
            result.setCode(200);
            result.setMsg("删除成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("删除失败!");
        }
        return result;
    }

    @GetMapping("/storey/one/{id}")
    public ResponseResult<Storey> one(@PathVariable("id") int id) {
        ResponseResult<Storey> result = new ResponseResult<>();
        try {
            Storey storey = storeyService.selectOne(id);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(storey);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");

}
        return result;
                }
    @GetMapping("/storey/selectAllList")
    public ResponseResult<List<Storey>> selectAllList() {
        ResponseResult<List<Storey>> result = new ResponseResult<>();
        try {
            List<Storey> list = storeyService.selectAllList();
            result.setCode(200);
            result.setData(list);
            result.setMsg("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("系统繁忙,请稍后再试");
            result.setCode(205);

        }
        return result;
    }
}
